/*
 * ScreenUnit.hpp
 *
 *  Created on: 2013-1-15
 *      Author: heqiu
 */

#ifndef SCREENUNIT_HPP_
#define SCREENUNIT_HPP_
#include "mycomm.h"
#include "MyPoint.hpp"
class ScreenUnit {
public:
	ScreenUnit() {
		m_pScreen = NULL;
		m_bMoveing = false;
		m_uiLastX = 0;
		m_uiLastY = 0;
		m_uiWinSize = 1;
		m_uiWidth = 0;
		m_uiHeight = 0;
	}
	int32_t init(uint32_t uiWidth, uint32_t uiHeight, uint32_t uiBpp,
			const char * pName) {
		m_uiWidth = uiWidth;
		m_uiHeight = uiHeight;
		m_pScreen = SDL_SetVideoMode(uiWidth, uiHeight, uiBpp, SDL_SWSURFACE);
		if (m_pScreen == NULL) {
			return -1;
		}
		SDL_WM_SetCaption(pName, NULL);
		return 0;
	}
	void white() {
		SDL_FillRect(m_pScreen, &m_pScreen->clip_rect,
				SDL_MapRGB(m_pScreen->format, 0xFF, 0xFF, 0xFF));
	}
	void show() {
		SDL_Flip(m_pScreen);
	}
	void mouseMoving(uint16_t uiX, uint16_t uiY) {
		if (m_bMoveing) {
			m_Pos.m_x -= (uiX - m_uiLastX);
			m_Pos.m_y -= (uiY - m_uiLastY);
			m_uiLastX = uiX;
			m_uiLastY = uiY;
		}
	}
	void mouseUp(uint16_t uiX, uint16_t uiY) {
		m_bMoveing = false;
	}
	void mouseDown(uint16_t uiX, uint16_t uiY) {
		m_bMoveing = true;
		m_uiLastX = uiX;
		m_uiLastY = uiY;
	}
	//获取换算的坐标点
	int32_t getWinPosX() {
		if (m_Pos.m_x > 0) {
			return m_Pos.m_x / (m_uiWinSize * 2) * 2;
		} else {
			return -((-m_Pos.m_x) / (m_uiWinSize * 2) * 2);
		}
	}
	int32_t getWinPosY() {
		if (m_Pos.m_y > 0) {
			return m_Pos.m_y / m_uiWinSize * 2;
		} else {
			return -((-m_Pos.m_y) / m_uiWinSize * 2);
		}
	}
	uint32_t getWinWidth() {
		return m_uiWidth / (m_uiWinSize * 2) * 2;
	}
	uint32_t getWinHeight() {
		return m_uiHeight / (m_uiWinSize) * 2;
	}
	SDL_Surface * m_pScreen;
	MyPoint m_Pos;
	uint32_t m_uiWidth;
	uint32_t m_uiHeight;
	uint32_t m_uiWinSize;
private:
	bool m_bMoveing;
	uint16_t m_uiLastX;
	uint16_t m_uiLastY;
};

#endif /* SCREENUNIT_HPP_ */
